/**
 * 
 */
$(initTask(),initreply());
function initTask(){
	$.ajax({
		type : "post",
		url : "GetCoopTaskServlet",
		data :{task_id:task_id},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			var pro=jsondata.taskArray[0];
			$("#talk_title").text(pro.Name);
			$("#creator_icon").attr("src","../userIcons/"+pro.cIcon);
			$("#author_name").text(pro.cName);
			$("#createdOn").text(pro.CreatedOn+" 创建");
			if(pro.Desc){
				$("#talkbody").children().remove();
				$("#talkbody").text(pro.Desc);
			}

			state=pro.State;
			setTools(pro.State);
			
		},
		error : function() {
			// TODO do something
		}
	});
	
}
function initreply(){
	$.ajax({
		type : "post",
		url : "GetCoopTaskReplyServlet",
		data :{task_id:task_id},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			var commentNode=$("#commentNode");
			commentNode.children().remove();
			$.each(jsondata.replyArray ,function (n,v){
				commentNode.append("<div class=\"comment\" id=\"reply_"+v.ID+"\" data-creator-guid=\"\">"+
						"<a class=\"avatar-wrap\" href=\"#\" target=\"_blank\"> <img class=\"avatar\""+
						"src=\"../userIcons/"+v.userIcon+"\" width=\"50\" height=\"50\">"+
					"</a>"+
					"<div class=\"comment-actions \">"+
						"<div class=\"actions\">"+
							"</div>" +
					"</div>"+
					"<div class=\"comment-main\">"+
						"<div class=\"info\">"+
							"<a class=\"author\" href=\"#\" data-stack=\"\" data-stack-root=\"\">"+v.userNickName+"</a> " +
							"<a class=\"create-time\" href=\"#\" title=\"\" data-readable-time=\"\">"+v.CreatedOn+"</a>"+
						"</div>"+
						"<div class=\"comment-content editor-style\">"+
							"<p>"+v.Body+"<br></p>"+
						"</div></div></div>");
			});
			
		},
		error : function() {
			// TODO do something
		}
	});
}
function showEditDesc(){
	$("#talkbody").children().remove();
	$("#talkbody").append("<textarea id=\"edit_desc\" name=\"edit_desc\" rows=\"20\" style=\"width: 99.4%;height:200px\"></textarea>");
	$("#talkbody").append("<div class=\"hide form-buttons\" style=\"display: block;\">"+
							"<button class=\"btn btn-primary btn-create-comment\"  onclick=\"postDesc()\" data-disable-with=\"正在发送...\" type=\"button\" tabindex=\"1\">设置</button>"+
							"<button class=\"btn btn-x btn-cancel-create-comment\" type=\"button\" onclick=\"clossEditdesc()\" tabindex=\"2\">取消</button>"+
						"</div>");
	$(function (){
		$("#talkbody").find("#edit_desc").xheditor(
				{
					plugins : getCodePlugin(),
					tools : "Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Emot,Table,Code,|,Source,Fullscreen",
					upImgUrl : "!../learning/uploadimg.jsp",
					loadCSS : "../css/editor.css"
				});
	});
}
function clossEditdesc(){
	$("#talkbody").children().remove();
	$("#talkbody").append("<a class=\"detail-action-edit link-add-desc\" data-loading=\"\" data-method=\"get\" data-remote=\"true\" href=\"javascript:;\" onclick=\"showEditDesc()\" >添加任务描述</a> <br>");
}
function postDesc(){
	var desc=$("#edit_desc").val();
	$.ajax({
		type : "post",
		url : "UpdateCoopTaskDescServlet",
		data :{task_id:task_id,desc:desc},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="true"){
				clossEditdesc();
				initreply();
			}else{
				confirm("添加 失败！ 请重新回复...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
	
}
function showEditarea(){
	if(state=="结束"){
		confirm("很抱歉，讨论已经结束了！！");
		return false;
	}
	$("#editnode").children().remove();
	$("#editnode").append("<textarea id=\"edit_area\" name=\"edit_area\" rows=\"20\" style=\"width: 99.4%;height:200px\"></textarea>");
	$("#editnode").append("<div class=\"hide form-buttons\" style=\"display: block;\">"+
							"<button class=\"btn btn-primary btn-create-comment\"  onclick=\"putReply()\" data-disable-with=\"正在发送...\" type=\"submit\" tabindex=\"1\">发表评论</button>"+
							"<button class=\"btn btn-x btn-cancel-create-comment\" type=\"button\" onclick=\"clossEditarea()\" tabindex=\"2\">取消</button>"+
						"</div>");
	$(function (){
		$("#editnode").find("#edit_area").xheditor(
				{
					plugins : getCodePlugin(),
					tools : "Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Emot,Table,Code,|,Source,Fullscreen",
					upImgUrl : "!../learning/uploadimg.jsp",
					loadCSS : "../css/editor.css"
				});
	});
	
}
function clossEditarea(){
	$("#editnode").children().remove();
	$("#editnode").append("<div class=\"form-item\"><div  class=\"form-field\"><div class=\"fake-textarea\" onclick=\"showEditarea()\" href=\"javascript:;\" data-droppable=\"\">点击发表评论</div></div></div>");
}
function putReply(){
	var body=$("#edit_area").val();
	if(!body||body==""){
		confirm("请输入 回复内容 ~~~");
		return false;
	}
	$.ajax({
		type : "post",
		url : "PutCoopTaskReplyServlet",
		data :{task_id:task_id,user_id:user_id,body:body},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="true"){
				confirm("回复 成功！！");
				clossEditarea();
				initreply();
			}else{
				confirm("回复 失败！ 请重新回复...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}
function setTools(state){
	var toolsNode=$("#toolsNode");
	toolsNode.children().remove();
	if(state=="未进行"){
		toolsNode.append("<div class=\"item\" data-visible-to=\"creator,admin\">"+
							"<a href=\"javascript:;\" onclick=\"updateTalkstate(task_id,'进行')\" class=\"detail-action detail-action-edit\""+
								"data-loading=\"true\" data-method=\"get\" data-remote=\"true\">标记为进行</a>"+
						"</div>");
	}else if(state=="进行"){
		toolsNode.append("<div class=\"item\" data-visible-to=\"creator,admin\">"+
				"<a href=\"javascript:;\" onclick=\"updateTalkstate(task_id,'结束')\" class=\"detail-action detail-action-edit\""+
					"data-loading=\"true\" data-method=\"get\" data-remote=\"true\">标记为结束</a>"+
			"</div>");
	}
	toolsNode.append("<div class=\"item\" data-visible-to=\"creator,admin\">"+
							"<a href=\"javascript:;\" onclick=\"deletTalk()\" class=\"detail-action detail-action-del\""+
								"data-confirm=\"确定要删除这条讨论吗？\" data-remote=\"true\""+
								"data-stack-replace=\"true\" rel=\"nofollow\">删除</a>"+
						"</div>");
}
function updateTalkstate(t_id,tyle){
	if(confirm("您确认标记为   "+tyle+" 吗？操作后无法恢复...")){
		$.ajax({
			type : "post",
			url : "UpdateCoopTaskStateServlet",
			data :{t_id:t_id,tyle:tyle},
			dateType : "json",
			success : function(data) {
				var jsondata = eval('(' + data + ')');
				if(jsondata.isSuccess=="true"){
					initTask();
				}else{
					confirm("操作 失败！请重试...");
				}
			},
			error : function() {
				// TODO do something
			}
		});
	}
	
}
function deletTask(){
	if(confirm("您确认删除该   讨论    吗？ 删除后无法恢复...")){
		$.ajax({
			type : "post",
			url : "",
			data :{task_id:task_id},
			dateType : "json",
			success : function(data) {
				var jsondata = eval('(' + data + ')');
				if(jsondata.isSuccess=="true"){
					history.back();
				}else{
					confirm("操作 失败！请重试...");
				}
			},
			error : function() {
				// TODO do something
			}
		});
	}
}